﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Repository;
using GoodMan.Infrastructure.Entities.Enum;
using GoodMan.Infrastructure.Entities.Cache;

namespace GoodMan.Infrastructure.Entities.Entity
{
    public class Product : IAccessible<T_Product, Product>, ILogicEntity<T_Product, Product>
    {
        public int Id { get; set; }
        public string ProductCode { get; set; }
        public string ProductName { get; set; }
        public int ProductType { get; set; }
        public int ProductSubType { get; set; }
        public string Images { get; set; }
        public string Description { get; set; }
        public string Version { get; set; }
        public DateTime CreateDate { get; set; }
        public AreaCode AreaCode { get; set; }//1：普通商品 2：团购商品 3：积分兑换商品 4：商城套餐区 10：快餐
        /// <summary>
        /// 评分
        /// </summary>
        public int Value { get; set; }
        /// <summary>
        /// 所属商户Id (快餐)
        /// </summary>
        public int BusinessId { get; set; }
        /// <summary>
        /// 配餐信息
        /// </summary>
        public string Catering { get; set; }
        public int CapsPeople { get; set; }
        public decimal PurchasePrice { get; set; }
        public decimal OriginalPrice { get; set; }

        public int TotalRemain { get; set; }
        public decimal DefaulSalePrice { get; set; }
        public decimal PolicyPrice { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string StateStr { get; set; }
        public string CategoryName { get; set; }
        public string SubCategoryName { get; set; }
        public int Integral { get; set; }
        public string Recomendedstr { get; set; }
        public int SaleCount { get; set; }

        public ProductCategory productCategory { get; set; }

        public Product LoadProductCategory(T_ProductCategory productCategory)
        {
            this.productCategory = new ProductCategory().Set(productCategory);
            return this;
        }

        public Policy policy { get; set; }

        public Product LoadPolicy(T_Policy policy)
        {
            this.policy = new Policy().Set(policy);
            return this;
        }

        public Business business { get; set; }

        public Product Loadbusiness(T_Business business)
        {
            this.business = new Business().Set(business);
            return this;
        }
        protected override void Covariant(T_Product model)
        {
            if (model != null)
            {
                this.AreaCode = (AreaCode)model.AreaCode;
                this.BusinessId = model.BusinessId;
                this.Catering = model.Catering;
                this.CreateDate = model.CreateDate;
                this.Description = model.Description;
                this.Id = model.Id;
                this.Images = model.Images;
                this.ProductCode = model.ProductCode;
                this.ProductName = model.ProductName;
                this.ProductSubType = model.ProductSubType;
                this.ProductType = model.ProductType;
                this.Value = model.Value;
                this.Version = model.Version;
                this.CapsPeople = model.CapsPeople;
                this.PurchasePrice = model.PurchasePrice;
                this.OriginalPrice = model.OriginalPrice;
            }
        }

        protected override T_Product Inverter(Product model)
        {
            if (model != null)
            {
                return new T_Product()
                {
                    AreaCode = (int)model.AreaCode,
                    BusinessId = model.BusinessId,
                    Catering = model.Catering,
                    CreateDate = model.CreateDate,
                    Description = model.Description,
                    Id = model.Id,
                    Images = model.Images,
                    ProductCode = model.ProductCode,
                    ProductName = model.ProductName,
                    ProductSubType = model.ProductSubType,
                    ProductType = model.ProductType,
                    Value = model.Value,
                    Version = model.Version,
                    CapsPeople=model.CapsPeople,
                    PurchasePrice=model.PurchasePrice,
                    OriginalPrice=model.OriginalPrice
                };
            }
            return null;
        }

        public Product Set(T_Product dbModel)
        {
            Covariant(dbModel);
            return this;
        }

        public override Product Find(string key)
        {
            return new ProductCache().GetItem(key);
        }
        public override Product PostDelete()
        {
            new ProductCache().RefreshItem(Id.ToString());
            return base.PostDelete();
        }
        public override Product PostModify()
        {
            new ProductCache().RefreshItem(Id.ToString());
            return base.PostModify();
        }
    }
}
